package cn.edu.scau.cmi.zhonghaozheng.gui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cn.edu.scau.cmi.zhonghaozheng.adapter.AdapterOfclass;
import cn.edu.scau.cmi.zhonghaozheng.adapter.Student;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class AdapterGUI {
	List<Student> students = new ArrayList<Student>();
	List<Student> afterSort = new ArrayList<Student>();
	public AdapterGUI(Stage primaryStage) {
		AdapterOfclass adapter = new AdapterOfclass();
		final TextField inputNo = new TextField();
		final TextField inputName = new TextField();
		final Button comfirm = new Button("确认"); 
		final Label warning = new Label();
		final Label showResult = new Label();
		inputNo.setPromptText("输入学号");
		inputName.setLayoutX(200);
		inputName.setPromptText("输入姓名");
		comfirm.setLayoutX(400);//400
		warning.setLayoutY(30);//30
		showResult.setLayoutX(100);//100
		showResult.setLayoutY(100);//100
		comfirm.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				if(inputNo.getText().equals("")?true:false||inputName.getText().equals("")?true:false) {
					warning.setText("学号或姓名不能为空！");
				}
				else {
					warning.setText("");
					students.add(new Student(Integer.parseInt(inputNo.getText()),inputName.getText()));
					afterSort = adapter.sortStudent(students);
					Iterator iterator = afterSort.iterator();
					String res = "";
					while(iterator.hasNext()) {
						res += iterator.next().toString()+'\n';
					}
					showResult.setText(res);
				}
			}
		});
		
		
		
		
        Group root = new Group();
        root.getChildren().add(inputNo);
        root.getChildren().add(inputName);
        root.getChildren().add(comfirm);
        root.getChildren().add(warning);
        root.getChildren().add(showResult);
        primaryStage.setScene(new Scene(root, 500, 300));
        primaryStage.setTitle("Adapter");
        primaryStage.show();
	}
}
